<?php
namespace Home\Controller;
use Think\Controller;
class UploadController extends Controller {

    //TP构造函数
    public function _initialize(){
        //自动加载的类
        new \Org\Autoload();
        if(session('member_id')==''){
            echo "<script>alert('您没有登录，请您登录后再访问此页面');window.location.href='/user/login';</script>";exit;
        }
        $this->member_id=$this->member_info['member_id'];
        //echo $member_id;exit;
    }

    public function index(){

        $data['title']='资源单 找金银网';
        $data['active']='upload';
        if(session('member_id')){
            $member_id=session('member_id');
        }
        $this->assign("data",$data);
        $this->assign("member_id", $member_id);
        $this->display();
    }

    public function sheet_post(){
        $post=I('post.');
        $member_id=$post['member_id'];
        //echo $member_id;exit;
        if(!empty($_FILES['quotation_file']['tmp_name'])){
            $root_path=ROOT_PATH.'www/data/upload/quotation/'.$member_id;
            //echo $root_path;exit;
            if(!is_dir($root_path)){
                mkdir($root_path);
            }
            //上传图片
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize=2048000;// 设置附件上传大小
            $upload->exts=array('xls','xlsx');// 设置附件上传类型
            $upload->rootPath=$root_path.'/'; // 设置附件上传根目录
            $upload->savePath=''; // 设置附件上传（子）目录
            $upload->subName='';
            $upload->saveName=md5(time());
            $info=$upload->upload();
            if(!$info) {// 上传失败
                echo "<script>alert('上传失败，请您重新上传');history.back(-1);</script>";exit;
            }
            $quotation_file='/data/upload/quotation/'.$member_id.'/'.$info['quotation_file']['savename'];
            $post['quotation_file']=$quotation_file;
        }
        $post['member_id']=$this->member_id;
        $post['quotation_time']=time();
        $post['quotation_sn']=date('YmdHis',time()).rand(001,999);
        //print_r($post);exit;
        $Model=D("Quotation");
        $Model->post=$post;


        if(!$Model->quotation_add_post()){
            echo "<script>alert('提交失败，请您尝试重新提交');history.back(-1);</script>";exit;
        }
        echo "<script>alert('提交成功');window.location.href='/upload/'</script>";
    }
}